package md.mru.core.service;

import md.mru.core.common.BaseDtoMapper;
import md.mru.core.domains.*;
import md.mru.core.dtos.*;
import md.mru.core.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sergiu.marin
 * @since 01.04.2014
 */
@Service
public class EmploymentDataService {

    @Autowired
    @Qualifier(value = "dtoMapper")
    private BaseDtoMapper mapper;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private ContractAgreementRepository contractAgreementRepository;
    @Autowired
    private WorkExperienceRepository workExperienceRepository;
    @Autowired
    private SalaryRepository salaryRepository;
    @Autowired
    private WorkTypeRepository workTypeRepository;
    @Autowired
    private SalaryLevelRepository salaryLevelRepository;
    @Autowired
    private SalaryStepRepository salaryStepRepository;


    public List<ContractDto> getContracts(Long folderId) {
        List<Contract> contracts = contractRepository.getContracts(folderId);
        return mapper.mapList(contracts, ContractDto.class);
    }

    public long saveContract(ContractDto contractDto) {
        Contract contract = mapper.map(contractDto, Contract.class);
        return contractRepository.save(contract).getId();
    }

    public void deleteContract(ContractDto contractDto) {
        Contract contract = mapper.map(contractDto, Contract.class);
        contractRepository.delete(contract);
    }

    public List<ContractAgreementDto> getContractAgreements(Long contractId) {
        List<ContractAgreement> contractAgreements = contractAgreementRepository.findAll();
        return mapper.mapList(contractAgreements, ContractAgreementDto.class);
    }

    public long saveContractAgreement(ContractAgreementDto contractAgreementDto) {
        ContractAgreement contractAgreement = mapper.map(contractAgreementDto, ContractAgreement.class);
        return contractAgreementRepository.save(contractAgreement).getId();
    }

    public void deleteContractAgreement(ContractAgreementDto contractAgreementDto) {
        ContractAgreement contractAgreement = mapper.map(contractAgreementDto, ContractAgreement.class);
        contractAgreementRepository.delete(contractAgreement);
    }

    public List<WorkExperienceDto> getWorkExperiences(Long folderId) {
        List<WorkExperience> workExperiences = workExperienceRepository.getWorkExperiences(folderId);
        return mapper.mapList(workExperiences, WorkExperienceDto.class);
    }

    public long saveWorkExperience(WorkExperienceDto workExperienceDto) {
        WorkExperience workExperience = mapper.map(workExperienceDto, WorkExperience.class);
        return workExperienceRepository.save(workExperience).getId();
    }

    public void deleteWorkExperience(WorkExperienceDto workExperienceDto) {
        WorkExperience workExperience = mapper.map(workExperienceDto, WorkExperience.class);
        workExperienceRepository.delete(workExperience);
    }

    public List<SalaryDto> getSalaries(Long folderId) {
        List<Salary> salaries = salaryRepository.getSalaries(folderId);
        return mapper.mapList(salaries, SalaryDto.class);
    }

    public long saveSalary(SalaryDto salaryDto) {
        Salary salary = mapper.map(salaryDto, Salary.class);
        return salaryRepository.save(salary).getId();
    }

    public void deleteSalary(SalaryDto salaryDto) {
        Salary salary = mapper.map(salaryDto, Salary.class);
        salaryRepository.delete(salary);
    }

    public List<WorkTypeDto> getWorkCategories() {
        List<WorkType> workCategories = workTypeRepository.findAll();
        return mapper.mapList(workCategories, WorkTypeDto.class);
    }

    public List<SalaryLevelDto> getSalaryLevels() {
        List<SalaryLevel> salaryLevels = salaryLevelRepository.findAll();
        return mapper.mapList(salaryLevels, SalaryLevelDto.class);
    }

    public List<SalaryStepDto> getSalarySteps() {
        List<SalaryStep> salarySteps = salaryStepRepository.findAll();
        return mapper.mapList(salarySteps, SalaryStepDto.class);
    }
}
